package com.yipei.business.service.impl;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yipei.business.vo.*;
import com.yipei.common.utils.DateUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yipei.business.mapper.MlOptionalSymbolMapper;
import com.yipei.business.domain.MlOptionalSymbol;
import com.yipei.business.service.IMlOptionalSymbolService;
import org.springframework.util.ObjectUtils;

/**
 * 我的自选Service业务层处理
 *
 * @author yipei
 * @date 2024-12-21
 */
@Service
public class MlOptionalSymbolServiceImpl extends ServiceImpl<MlOptionalSymbolMapper, MlOptionalSymbol> implements IMlOptionalSymbolService
{
    @Autowired
    private MlOptionalSymbolMapper mlOptionalSymbolMapper;

    /**
     * 查询我的自选
     *
     * @param id 我的自选主键
     * @return 我的自选
     */
    @Override
    public MlOptionalSymbol selectMlOptionalSymbolById(Long id)
    {
        return mlOptionalSymbolMapper.selectMlOptionalSymbolById(id);
    }

    /**
     * 查询我的自选列表
     *
     * @param mlOptionalSymbol 我的自选
     * @return 我的自选
     */
    @Override
    public List<MlOptionalSymbol> selectMlOptionalSymbolList(MlOptionalSymbol mlOptionalSymbol)
    {
        return mlOptionalSymbolMapper.selectMlOptionalSymbolList(mlOptionalSymbol);
    }

    /**
     * 新增我的自选
     *
     * @param mlOptionalSymbol 我的自选
     * @return 结果
     */
    @Override
    public int insertMlOptionalSymbol(MlOptionalSymbol mlOptionalSymbol)
    {
        mlOptionalSymbol.setCreateTime(DateUtils.getNowDate());
        return mlOptionalSymbolMapper.insertMlOptionalSymbol(mlOptionalSymbol);
    }

    /**
     * 修改我的自选
     *
     * @param mlOptionalSymbol 我的自选
     * @return 结果
     */
    @Override
    public int updateMlOptionalSymbol(MlOptionalSymbol mlOptionalSymbol)
    {
        mlOptionalSymbol.setUpdateTime(DateUtils.getNowDate());
        return mlOptionalSymbolMapper.updateMlOptionalSymbol(mlOptionalSymbol);
    }

    /**
     * 批量删除我的自选
     *
     * @param ids 需要删除的我的自选主键
     * @return 结果
     */
    @Override
    public int deleteMlOptionalSymbolByIds(Long[] ids)
    {
        return mlOptionalSymbolMapper.deleteMlOptionalSymbolByIds(ids);
    }

    /**
     * 删除我的自选信息
     *
     * @param id 我的自选主键
     * @return 结果
     */
    @Override
    public int deleteMlOptionalSymbolById(Long id)
    {
        return mlOptionalSymbolMapper.deleteMlOptionalSymbolById(id);
    }

    @Override
    public List<OptionalSymbolVo> createOptionalList(String symbol, OptionalSymbolVo optionalSymbolVo) throws IOException {
        List<OptionalSymbolVo> optionalSymbolVoList=new ArrayList<>();
        String code="";
        if(symbol.equals("000001")){
             code = "sz"+symbol+",bj"+symbol; // 股票代码（沪市:sh600000，深市:sz000001）
        }else {
             code = "sh"+symbol+",sz"+symbol+",bj"+symbol; // 股票代码（沪市:sh600000，深市:sz000001）
        }
        HttpClient client3 = HttpClients.createDefault();
        HttpGet request3 = new HttpGet("http://sqt.gtimg.cn/q=" + code); //示例URL，请替换为实际的股票代码
        request3.setHeader("Content-Type", "text/javascript;charset=UTF-8");
        HttpResponse response = client3.execute(request3);
        if (response.getStatusLine().getStatusCode() == 200) {
            HttpEntity entity = response.getEntity();
            String raw = EntityUtils.toString(entity, "UTF-8").replace("jQuery", "");
            // 提取引号内的数据
            int firstQuote = raw.indexOf("\"");
            int lastQuote = raw.lastIndexOf("\"");
            String data = raw.substring(firstQuote + 1, lastQuote);
            String[] fields = data.split("~");
            String name = fields[1];
            String stockCode = fields[2];
            String price = fields.length > 3 ? fields[3] : "";
            String changePercent = fields.length > 32 ? fields[32] : "";
            String increase =fields.length>31?fields[31]:"";
            optionalSymbolVo.setSymbol(stockCode);
            optionalSymbolVo.setSec_name(name);
            optionalSymbolVo.setF12(stockCode);
            optionalSymbolVo.setF14(name);
            optionalSymbolVo.setIncrease(increase);
            optionalSymbolVo.setPrice(price);
            optionalSymbolVo.setRatio(changePercent+"%");
            if (!ObjectUtils.isEmpty(optionalSymbolVo)){
                optionalSymbolVoList.add(optionalSymbolVo);
            }
        }
        return optionalSymbolVoList;
    }
}
